package com.tjmc.recruit.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjmc.recruit.admin.api.entity.SessionInfoEntity;
import com.tjmc.recruit.admin.api.vo.ChatVO;
import com.tjmc.recruit.common.core.constant.CommonConstants;
import com.tjmc.recruit.common.core.constant.SecurityConstants;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.tjmc.recruit.admin.service.SessionInfoService;

import com.tjmc.recruit.common.security.annotation.Inner;
import com.tjmc.recruit.finance.api.entity.PlatformUserAccountEntity;
import com.tjmc.recruit.finance.api.feign.RemoteAccountService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.tjmc.recruit.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 会话信息表
 *
 * @author recruit
 * @date 2024-12-03 16:02:27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sessionInfo" )
@Tag(description = "sessionInfo" , name = "会话信息表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SessionInfoController {

    private final  SessionInfoService sessionInfoService;
    private final RemoteAccountService remoteAccountService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sessionInfo 会话信息表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R getSessionInfoPage(@ParameterObject Page page, @ParameterObject SessionInfoEntity sessionInfo) {
        LambdaQueryWrapper<SessionInfoEntity> wrapper = Wrappers.lambdaQuery(sessionInfo);
        return R.ok(sessionInfoService.page(page, wrapper));
    }


    /**
     * 通过条件查询会话信息表
     * @param sessionInfo 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    public R getDetails(@ParameterObject SessionInfoEntity sessionInfo) {
        return R.ok(sessionInfoService.list(Wrappers.query(sessionInfo)));
    }

    /**
     * 查询当前用户会话人员列表
     * @return R
     */
    @Operation(summary = "查询当前用户会话人员列表" , description = "查询当前用户会话人员列表" )
    @SysLog("查询当前用户会话人员列表" )
    @GetMapping("/userList")
    public R queryCurrentUserList() {
        return R.ok(sessionInfoService.queryCurrentUserSessionList());
    }

    /**
     * 查询当前用户与个人会话记录列表
     * @return R
     */
    @Operation(summary = "查询当前用户与个人会话记录列表" , description = "查询当前用户与个人会话记录列表" )
    @SysLog("查询当前用户与个人会话记录列表" )
    @GetMapping("/sessionHistory/list")
    public R queryCurrentUserSessionList(@ParameterObject Page page, @ParameterObject Long userId) {
        return R.ok(sessionInfoService.querySessionInfoPage(page, userId));
    }

    /**
     * 在线聊天
     * @param chatVO
     * @return
     */
    @Operation(summary = "在线聊天" , description = "在线聊天" )
    @SysLog("在线聊天" )
    @PostMapping("/chat")
    public R chat(@ParameterObject ChatVO chatVO) {
        // 1. 查询该用户的账户E币余额
        R<PlatformUserAccountEntity> accountInfo = remoteAccountService.getAccountInfo(String.valueOf(chatVO.getAccountId()), SecurityConstants.FROM_IN);
        if (accountInfo.getCode() != CommonConstants.SUCCESS){
            return R.failed("未查询到账户信息");
        }
        PlatformUserAccountEntity data = accountInfo.getData();
        if (data == null){
            return R.failed("未查询到账户信息");
        }
        if (data.getECoinNum().compareTo(chatVO.getECoinNum()) < 0) {
            return R.failed("账户E币余额不足");
        }
        R r = sessionInfoService.chatOnLine(chatVO, data);
        if (r.getCode() != CommonConstants.SUCCESS){
            return R.failed(r.getMsg());
        }
        return R.ok();
    }

    @Operation(summary = "是否沟通过" , description = "是否沟通过" )
    @GetMapping("/isChat")
    @Inner
    public R isChat(@RequestParam String userId, @RequestParam String companyUserId) {
        List<SessionInfoEntity> list = sessionInfoService.list(Wrappers.<SessionInfoEntity>lambdaQuery()
                .eq(SessionInfoEntity::getSendUserId, companyUserId).eq(SessionInfoEntity::getReceiverUserId, userId));
        if (list.isEmpty()) {
            return R.failed();
        }
        return R.ok();
    }

}
